package com.crm.controller.saling;

import com.crm.pojo.CODetail;
import com.crm.pojo.Dispatcher;
import com.crm.pojo.DispatcherDetail;
import com.crm.service.saling.CODetailsService;
import com.crm.service.saling.ContractService;
import com.crm.service.saling.DispatcherDetailService;
import com.crm.service.saling.DispatcherService;
import com.crm.vo.CODetailsVO;
import com.crm.vo.DispatcherContractClientProductVO;
import com.crm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dr5hx on 2018/8/30
 */
@Controller
public class DispatcherDetailController {
    @Autowired
    private DispatcherDetailService dispatcherDetailService;
    @Autowired
    private DispatcherService dispatcherService;
    @Autowired
    private CODetailsService coDetailsService;
    @Autowired
    private ContractService contractService;

    /**
     * 显示发货详单
     *
     * @param page
     * @param rows
     * @return
     */
    @PostMapping("getDispatcherContractClientProductVOList")
    @ResponseBody
    public Map getDispatcherContractClientProductVOList(int page, int rows) {
        try {
            Long count = dispatcherDetailService.getDispatcherDetailsCount();
            List<DispatcherContractClientProductVO> list = dispatcherDetailService.getDispatcherContractClientProductVOList(page, rows);
            Map map = new HashMap();
            map.put("rows", list);
            map.put("total", count);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    @GetMapping("togetDispatcherContractClientProductVOList")
    public String togetDispatcherContractClientProductVOList() {
        return "saling/dispatcherDetailsList";
    }

    @PostMapping("completeDispatcher")
    @ResponseBody
    public Result completeDispatcher(Long dispatcherId) {
        try {
            Dispatcher dispatcher = dispatcherService.getDispatcherByDispatcherId(dispatcherId);
            if (!dispatcher.getDispatcherState().equals("完成")) {
                List<CODetailsVO> coDetailsVOList = coDetailsService.showOrderDetailsList(dispatcher.getFkOrderId().longValue());
                List<CODetail> coDetailList = new ArrayList<>();
                dispatcher.setDispatcherState("完成");
                List<DispatcherDetail> list = dispatcherDetailService.getDispatcherDetailListByDispatcherId(dispatcherId);
                for (DispatcherDetail dispatcherDetail : list) {
                    dispatcherDetail.setFkDispatcherId(new BigDecimal(2));
                    for (CODetailsVO coDetailsVO : coDetailsVOList) {
                        CODetail codetail = coDetailsVO.getCoDetail();
                        if (dispatcherDetail.getPId().equals(codetail.getCOProductId())) {
                            codetail.setProductType((new BigDecimal(codetail.getProductType()).subtract(dispatcherDetail.getProductAccount())).toString());
                            codetail.setCOCompleteAccount(dispatcherDetail.getProductAccount().
                                    add(codetail.getCOCompleteAccount()));
                            coDetailList.add(codetail);
                        }
                    }
                }
                dispatcherDetailService.saveDispatcherDetailList(list);
                coDetailsService.saveCoDetailList(coDetailList);
                dispatcherService.saveDispatcher(dispatcher);
                return Result.SUCCESS;
            } else {
                return new Result("操作已经完成拒绝执行");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("failed");
        }
    }
}
